/**
 * 组织架构管理相关API
 */
import { BaseApiService } from './base'
import request from '@/utils/request'
import type { Organize, OrganizeQuery, OrganizeTreeNode, PageResponse, ApiResponse } from '@/types'

export class OrganizeService extends BaseApiService<Organize> {
  constructor() {
    super('/organize')
  }

  /**
   * 获取组织架构树形结构
   */
  async getOrganizeTree(): Promise<ApiResponse<OrganizeTreeNode[]>> {
    return request.get<OrganizeTreeNode[]>('/organization/getAllOrg')
  }

  /**
   * 获取组织架构列表（分页）
   */
  async getOrganizeList(params?: OrganizeQuery): Promise<ApiResponse<PageResponse<Organize>>> {
    return request.get<PageResponse<Organize>>('/organize/list', params)
  }
  /**
   * 根据父级ID获取子组织
   */
  async getChildrenByParentId(parentId: number): Promise<ApiResponse<Organize[]>> {
    return request.get<Organize[]>(`/organize/children/${parentId}`)
  }

  /**
   * 获取组织架构详情
   */
  async getOrganizeDetail(id: number): Promise<ApiResponse<Organize>> {
    return request.get<Organize>(`/organize/${id}`)
  }

  /**
   * 创建组织架构
   */
  async createOrganize(data: Partial<Organize>): Promise<ApiResponse<Organize>> {
    return request.post<Organize>('organization/addOrganization', data)
  }

  /**
   * 更新组织架构
   */
  async updateOrganize(data: Partial<Organize>): Promise<ApiResponse<Organize>> {
    return request.post<Organize>(`/organization/updateOrg`, data)
  }

  /**
   * 删除组织架构
   */
  async deleteOrganize(id: number): Promise<ApiResponse<void>> {
    return request.delete(`/organization/deleteOrg?id=${id}`)
  }

  /**
   * 批量删除组织架构
   */
  async batchDeleteOrganizes(ids: number[]): Promise<ApiResponse<void>> {
    return request.post('/organize/batch-delete', { ids })
  }

  /**
   * 移动组织架构到新的父级
   */
  async moveOrganize(id: number, newParentId: number): Promise<ApiResponse<void>> {
    return request.put(`/organize/${id}/move`, { parentId: newParentId })
  }

  /**
   * 更新组织架构排序
   */
  async updateOrganizeSort(ids: number[]): Promise<ApiResponse<void>> {
    return request.put('/organize/sort', { ids })
  }

  /**
   * 获取组织架构下的用户列表
   */
  async getOrganizeUsers(id: number, params?: { page: number; pageSize: number }): Promise<ApiResponse<PageResponse<any>>> {
    return request.get<PageResponse<any>>(`/organize/${id}/users`, params)
  }

  /**
   * 获取组织架构下的设备列表
   */
  async getOrganizeDevices(id: number, params?: { page: number; pageSize: number }): Promise<ApiResponse<PageResponse<any>>> {
    return request.get<PageResponse<any>>(`/organize/${id}/devices`, params)
  }

  /**
   * 添加用户到组织架构
   */
  async addUserToOrganize(organizeId: number, userIds: number[]): Promise<ApiResponse<void>> {
    return request.post(`/organize/${organizeId}/users`, { userIds })
  }

  /**
   * 从组织架构移除用户
   */
  async removeUserFromOrganize(organizeId: number, userIds: number[]): Promise<ApiResponse<void>> {
    return request.delete(`/organize/${organizeId}/users`, { data: { userIds } })
  }

  /**
   * 添加设备到组织架构
   */
  async addDeviceToOrganize(organizeId: number, deviceIds: number[]): Promise<ApiResponse<void>> {
    return request.post(`/organize/${organizeId}/devices`, { deviceIds })
  }

  /**
   * 从组织架构移除设备
   */
  async removeDeviceFromOrganize(organizeId: number, deviceIds: number[]): Promise<ApiResponse<void>> {
    return request.delete(`/organize/${organizeId}/devices`, { data: { deviceIds } })
  }

  /**
   * 获取组织架构统计信息
   */
  async getOrganizeStats(id: number): Promise<ApiResponse<{
    userCount: number
    deviceCount: number
    subOrganizeCount: number
  }>> {
    return request.get(`/organize/${id}/stats`)
  }

  /**
   * 搜索组织架构
   */
  async searchOrganizes(keyword: string): Promise<ApiResponse<Organize[]>> {
    return request.get<Organize[]>('/organize/search', { keyword })
  }

  /**
   * 导出组织架构
   */
  async exportOrganizes(params?: OrganizeQuery): Promise<Blob> {
    const response = await request.get('/organize/export', {
      ...params,
      responseType: 'blob'
    })
    return response.data
  }

  /**
   * 导入组织架构
   */
  async importOrganizes(file: File): Promise<ApiResponse<{
    success: number
    failed: number
    errors: string[]
  }>> {
    const formData = new FormData()
    formData.append('file', file)
    return request.post('/organize/import', formData)
  }

  /**
   * 验证组织架构编码是否唯一
   */
  async validateCode(code: string, excludeId?: number): Promise<ApiResponse<{ valid: boolean }>> {
    return request.get('/organize/validate-code', { code, excludeId })
  }

  /**
   * 获取组织架构层级路径
   */
  async getOrganizePath(id: number): Promise<ApiResponse<Organize[]>> {
    return request.get<Organize[]>(`/organize/${id}/path`)
  }
}

// 创建服务实例
export const organizeService = new OrganizeService()

// 导出默认实例
export default organizeService